﻿// ----------------------------------------------------------------------
// <author name="Moses Kehengu"/>
// <copyright file="AddReport.cs" company="Futures Group International">
//     Copyright statement. All right reserved
// </copyright>
//
// ------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataModels.Common;
using GenericTools;

namespace ReportingManagement
{
    public partial class AddReport : Form
    {
        #region Class Variables
        private UserInformation currentUser;
        #endregion

        #region Constructors
        public AddReport(UserInformation user)
        {
            InitializeComponent();
            this.currentUser = user;
        }
        #endregion

        #region Event Handlers
        private void btnCreateReport_Click(object sender, EventArgs e)
        {
            #region Validation
            if (String.IsNullOrEmpty(txtReportName.Text))
            {
                MessageBox.Show(this, "Please provide report name.",
                    String.Format("{0} - Error", AppConfiguration.ApplicationName),
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtReportName.Focus();
                return;
            }

            if (String.IsNullOrEmpty(txtReportVersion.Text))
            {
                MessageBox.Show(this, "Please provide Report Version/Report SMS keyword",
                    String.Format("{0} - Error", AppConfiguration.ApplicationName),
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtReportVersion.Focus();
                return;
            }

            if (txtReportVersion.Text.Length > Utilities.MAXIMUM_REPVERSION)
            {
                MessageBox.Show(this, 
                    String.Format("Report Version/Report SMS keyword is very long. Maximum allowed is {0}",
                        Utilities.MAXIMUM_REPVERSION),
                    String.Format("{0} - Error", AppConfiguration.ApplicationName),
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtReportVersion.Focus();
                return;
            }

            try
            {
                int count = Int32.Parse(txtComponentCount.Text);
                if (count < Utilities.MINIMUM_DATA_COMPONENTS)
                {
                    MessageBox.Show(this,
                        String.Format("You have provided few data components count. Minimum allowed is {0}",
                            Utilities.MINIMUM_DATA_COMPONENTS),
                        String.Format("{0} - Error", AppConfiguration.ApplicationName),
                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtComponentCount.Focus();
                    return;
                }
            }
            catch
            {
                MessageBox.Show(this, "Please provide valid data component count",
                    String.Format("{0} - Error", AppConfiguration.ApplicationName),
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtComponentCount.Focus();
                return;
            }
            #endregion

            DialogResult response = MessageBox.Show(this,
                "Are you sure you want to  create a new report",
                AppConfiguration.ApplicationName,
                MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (response == System.Windows.Forms.DialogResult.No) return;

            try
            {
                bool usable = false;
                Cursor.Current = Cursors.WaitCursor;
                usable = Utilities.IsRepVersionUsable(txtReportVersion.Text);
                Cursor.Current = Cursors.Default;
                if (usable)
                {
                    string name = txtReportName.Text;
                    string repVersion = txtReportVersion.Text;
                    int dataCount = Int32.Parse(txtComponentCount.Text);
                    string description = txtDescription.Text;
                    string comments = txtComments.Text;
                    bool usedAsView = chbReportView.Checked;
                    int actionUserId = this.currentUser.ID;

                    Cursor.Current = Cursors.WaitCursor;
                    bool success = ChokaMbaya.CreateReport(name, repVersion, dataCount, 
                        description, comments, usedAsView, actionUserId);
                    Cursor.Current = Cursors.Default;
                    if (success)
                    {
                        MessageBox.Show(this,
                            "You have successfully create a new report",
                            AppConfiguration.ApplicationName,
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show(this,
                            "Creation of new report did not complete successfully",
                            String.Format("{0} - Error", AppConfiguration.ApplicationName),
                            MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show(this,
                        "Selected keyword is already in use. Please choose another Report Version/Report SMS keyword",
                        String.Format("{0} - Error", AppConfiguration.ApplicationName),
                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtReportVersion.Focus();
                    return;
                }
            }
            catch (Exception ex)
            {
                DisplayErrorMessage(ex);
                this.Close();
            }
        }
        #endregion

        #region Private Helper Methods
        private void DisplayErrorMessage(Exception ex)
        {
            Cursor.Current = Cursors.Default;
            string message = String.Format("{0}\n\nError Details:\n{1}",
                "Please make sure you have internet connection", ex.Message);
            MessageBox.Show(this, message, AppConfiguration.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }
        #endregion
    }    
}
